import Vue from 'vue'
import Vuex from 'vuex'

import {USER_TYPE, MODAL_TYPE, STORAGE_KEY} from '@/utils/constants'
import storage from '../utils/storage'
import {getInfo} from '@/network/user'


Vue.use(Vuex);

const stores = {
    state: {
        modalStatus: MODAL_TYPE.OFFLINE,
        userInfo: {
            userId: null,
            userName: null,
            userType: null,
            avatar: null,
            email: null,
            telphone: null
        },
    },
    getters: {
        isAdminRole: function (state) {
            // if (USER_TYPE.ADMIN === undefined || USER_TYPE.ADMIN === null)
            //     return false;
            console.log(state.userInfo);
            console.log(USER_TYPE.ADMIN);
            return state.userInfo.userType === USER_TYPE.ADMIN_USER
        },
        isSuperAdminRole: function (state) {
            return state.userInfo.userType === USER_TYPE.SUPER_ADMIN
        }
    },
    mutations: {
        setProfile(state, payload) {
            console.log(payload);
            this.state.userInfo = payload;
            state.modalStatus = MODAL_TYPE.ONLINE;
            storage.set(STORAGE_KEY.AUTHENTICATE, true)

        },
        clearProfile(state) {
            storage.set(STORAGE_KEY.AUTHENTICATE, false);
            state.modalStatus = MODAL_TYPE.OFFLINE;
            state.userInfo = {
                userId: null,
                userName: null,
                userType: null,
                avatar: null,
                email: null
            }
        }
    },
    actions: {
        getProfile(context) {

            getInfo().then(res => {
                context.commit('setProfile', res.data)
            }).catch(_ => {
                context.commit('clearProfile')
            })
        }
    }
};

const store = new Vuex.Store(stores);

Vue.prototype.$store = store;

export default store
